class Unit:

    name = ""
    parent = ""
    cost = 0
    speed = 0
    movetype = "L"
    frontArmor = 0
    rearArmor = 0
    structBoxes = 0
    damageBoxes = 0
    sensors = 0
    senRange = 0
    comms = 0
    commsRange = 0
    actions = 1
    mass = 6
    traitS = ""
    weaponS = ""
    weaponL = []
        
    def __init__(self):
        name = "foo"

    def parse(self, line):
        print "Parsing [%s]" % line
        
        idx = line.index(",")
        self.name = line[0:idx]
        tmp = line[idx+1:]

        idx = tmp.index(",")
        self.parent = tmp[0:idx]
        tmp = tmp[idx+1:]

        idx = tmp.index(",")
        self.cost = tmp[0:idx]
        tmp = tmp[idx+1:]

        idx = tmp.index(",")
        self.speed = tmp[0:idx].replace("\"","")
        if self.speed != '':
            self.speed = int(self.speed)
        else:
            self.speed = 0
        tmp = tmp[idx+1:]

        if tmp[0] == "\"":
            idx = tmp.index("\"", 1)
            self.movetype = tmp[1:idx]
            tmp = tmp[idx+2:]
        else:
            idx = tmp.index(",") 
            self.movetype = tmp[0:idx]
            tmp = tmp[idx+1:] 

        idx = tmp.index(",")
        self.frontArmor = tmp[0:idx]
        if self.frontArmor != "":
            self.frontArmor = int(self.frontArmor)
        else:
            self.frontArmor = 0
        tmp = tmp[idx+1:]

        idx = tmp.index(",")
        idx2 = tmp.index(",", idx+1)
        self.rearArmor = tmp[idx+1:idx2]
        if self.rearArmor != "":
            self.rearArmor = int(self.rearArmor)
        else:
            self.rearArmor = 0
        tmp = tmp[idx2+1:]

        idx = tmp.index(",")
        self.structBoxes = tmp[0:idx]
        if self.structBoxes != "" and self.damageBoxes != "-":
            self.structBoxes = int(self.structBoxes)
        else:
            self.structBoxes = 0
        tmp = tmp[idx+1:]

        idx = tmp.index(",")
        self.damageBoxes = tmp[0:idx]
        if self.damageBoxes != "" and self.damageBoxes != "-":
            self.damageBoxes = int(self.damageBoxes)
        else:
            self.damageBoxes = 0
        tmp = tmp[idx+1:]


        idx = tmp.index(",")
        self.sensors = tmp[0:idx]
        if self.sensors != "":
            self.sensors = int(self.sensors)
        else:
            self.sensors = 0
        tmp = tmp[idx+1:]

        idx = tmp.index(",")
        self.senRange = tmp[0:idx]
        self.senRange = self.senRange.replace("\"","")
        if self.senRange != "":
            self.senRange = int(self.senRange)
        else:
            self.senRange = 0
        tmp = tmp[idx+1:]

        idx = tmp.index(",")
        self.comms = tmp[0:idx]
        if self.comms != "":
            self.comms = int(self.comms)
        else:
            self.comms = 0
        tmp = tmp[idx+1:]

        idx = tmp.index(",")
        self.commsRange = tmp[0:idx]
        self.commsRange = self.commsRange.replace("\"","")
        if self.commsRange != "":
            self.commsRange = int(self.commsRange)
        else:
            self.commsRange = 0
        tmp = tmp[idx+1:]

        idx = tmp.index(",")
        self.actions = tmp[0:idx]
        if self.actions != "":
            self.actions = int(self.actions)
        else:
            self.actions = 0
        tmp = tmp[idx+1:]

        idx = tmp.index(",")
        self.mass = tmp[0:idx]
        if self.mass != "":
            self.mass = int(self.mass)
        else:
            self.mass = 0
        tmp = tmp[idx+1:]

        # Traits
        if tmp[0] == "\"":
            idx = tmp.index("\"", 1)
            self.traitS = tmp[1:idx]
            tmp = tmp[idx+2:]
        else:
            idx = tmp.index(",")
            tmp = tmp[idx+1:]
    
        # Weapons
        if tmp[0] == "\"":
            idx = tmp.index("\"", 1)
            self.weaponS = tmp[1:idx]
            weapons = self.weaponS.split(",")
            for weapon in weapons:
                weapon = weapon.strip()
                ammo = 0
                link = 0
                tokens = weapon.split("_")
                if len(tokens) >= 2:
                    ammo = int(tokens[1][1:2])
                if len(tokens) >= 3:
                    link = int(tokens[2][1:2])
                     
                print "Found weapon: %s - A%i L%i" % (tokens[0], ammo, link)
            tmp = tmp[idx+2:]
        else:
            idx = tmp.index(",")
            tmp = tmp[idx+1:]

    def printDetails(self):
        print "Name: (%s) Parent: (%s) Cost: (%s) Move: (%i %s) Armor: (%s / %s) Sensors: (%i / %i\") Comms (%i / %i\")\n  \
            Actions: (%i) Mass: (%i) Traits: (%s) Weapons: (%s)" % \
            (self.name, self.parent, self.cost, self.speed, self.movetype, self.frontArmor, self.rearArmor, self.sensors, self.senRange, 
             self.comms, self.commsRange, self.actions, self.mass, self.traitS, self.weaponS)
         
    
